//
//  PpmainVC.swift
//  Palmpilot
//
//  Created by 杨伟毅 on 2018/10/19.
//  Copyright © 2018年 杨伟毅. All rights reserved.
//

import UIKit
import QorumLogs

// 海拔仪  气压计  指南针  扫一扫  OCR识别手机号、身份证  记事本

class PpmainVC: PpBaseViewController,UIPopoverPresentationControllerDelegate,UITableViewDelegate,UITableViewDataSource {
    let baseView = UITableView.init(frame: ZCGRECT(x: 0, y: 0, width: SCREEN_width, height: SCREEN_HEIGHT-TopHeight), style: .plain)
    var  titleArr = [String]()
    
    
    override func viewWillAppear(_ animated: Bool) {
        self.title = PpLanguageSwitchTool.getString(key: "firstNavTitle")
        let leftBar = UIBarButtonItem.init(image: UIImage.init(named: "switch")?.withRenderingMode(.alwaysOriginal), style: UIBarButtonItemStyle.plain, target: self, action: #selector(changeLangueFunc))
        self.navigationItem.leftBarButtonItem = leftBar
        let rightbar = UIBarButtonItem.init(image: UIImage.init(named: "set")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(setCenter))
        self.navigationItem.rightBarButtonItem = rightbar
    }
    
   @objc func setCenter() -> Void {
       self.navigationController?.pushViewController(PpSetCentViewController(), animated:false)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        baseView.delegate = self
        baseView.dataSource = self
        baseView.tableFooterView = UIView()
        baseView.estimatedRowHeight = 100
        baseView.separatorStyle = .none
        baseView.register(UINib.init(nibName: "PpMainTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "PpMainTableViewCell")
        PpBaseTool.insetTableViewAndUIViewController(baseView: baseView, baseVC: self)
        titleArr =  [PpLanguageSwitchTool.getString(key: "Elevation of the instrument"),PpLanguageSwitchTool.getString(key: "The compass"),PpLanguageSwitchTool.getString(key: "Scan"),PpLanguageSwitchTool.getString(key: "Outdoor knowledge"),PpLanguageSwitchTool.getString(key: "Outdoor notes")]
        
        let now = Date()
        let nowFormtter = DateFormatter()
        nowFormtter.dateFormat = "yyyyMMdd"
        let nowStr = nowFormtter.string(from: now)
        self.view.addSubview(baseView)
        
    }
    @objc func changeLangueFunc(){
        let p = PpLanguageSwitchVC()
        p.PpSwiftchSelected = { (number) in
            p.dismiss(animated: false, completion: nil)
            if number == 0 {
                PpLanguageSwitchTool.sharedTool.setLanguage(langeuage: "zh-Hans")
            }else{
                PpLanguageSwitchTool.sharedTool.setLanguage(langeuage: "en")
            }
            self.title = PpLanguageSwitchTool.getString(key: "firstNavTitle")
            self.titleArr =  [PpLanguageSwitchTool.getString(key: "Elevation of the instrument"),PpLanguageSwitchTool.getString(key: "The compass"),PpLanguageSwitchTool.getString(key: "Scan"),PpLanguageSwitchTool.getString(key: "Outdoor knowledge"),PpLanguageSwitchTool.getString(key: "Outdoor notes")]
            self.baseView.reloadData()
        }
        p.preferredContentSize = CGSize.init(width: 150, height: 90)
        p.modalPresentationStyle = .popover
        p.popoverPresentationController?.barButtonItem = self.navigationItem.leftBarButtonItem
        p.popoverPresentationController?.permittedArrowDirections = .up
        p.popoverPresentationController?.delegate = self
        self.present(p, animated: true, completion: nil)
    }
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
    }
    func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
        return true
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titleArr.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let basecell:PpMainTableViewCell? = tableView.dequeueReusableCell(withIdentifier: "PpMainTableViewCell") as? PpMainTableViewCell
        basecell?.baseLabel.text = titleArr[indexPath.row]
        basecell?.selectionStyle = .none
        return basecell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch(indexPath.row) {
        case 0:
                self.navigationController?.pushViewController(PpAltitudinalViewController(), animated: true)
            break
        case 1:
             self.navigationController?.pushViewController(CompassViewController(), animated: true)
            break
        case 2:
           self.navigationController?.pushViewController(SWQRCodeViewController(), animated: true)
            break
        case 3:
            //
            self.navigationController?.pushViewController(PpOutDoorViewController(), animated: true)
            break
        case 4:
            self.navigationController?.pushViewController(PpOutDoorNotesViewController(), animated: true)
            break
//        case 5:
//            self.navigationController?.pushViewController(PpAttentionViewController(), animated: true)
//            break
        default:
            break
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
